/*
 *  Copyright (c) 2013.
 *  * 游戏服务器核心代码编写人陈磊拥有使用权
 *  * 联系方式：E-mail:13638363871@163.com ;qq:502959937
 *  * 个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.controller.handleController.rewardController;

import com.dc.gameserver.exceptions.dataException;
import com.dc.gameserver.proto.itemserverprotoVo.achiveAllStatrsRewardRequest;
import com.dc.gameserver.proto.itemserverprotoVo.achiveAllStatrsRewardResponse;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRoleArea;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 石头哥哥 on 13-12-23.
   地区满星奖励，恢复神识值
 */
@Service
public class achiveAreaAllStatrsRewardController extends AbstractController {

    @Resource
    private ModelCacheService modelCacheService;
    @Resource
    private PlayerService playerService;

    @Override
    public void PostConstruct() {
        int ID= achiveAllStatrsRewardRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]= achiveAllStatrsRewardRequest.getDefaultInstance();
    }

    /**
     * messageLite数据结构体分发
     *
     * @param messageLite 数据载体
     * @param player      active object
     * @throws Exception 注意messageLite应该递交给GC直接处理  ，set null
     */
    @Override
    @SuppressWarnings("unchecked")
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        achiveAllStatrsRewardRequest achiveAllStatrsRewardRequest= (achiveAllStatrsRewardRequest) messageLite;
        int areaID=achiveAllStatrsRewardRequest.getAreaID();
        achiveAllStatrsRewardRequest=null;
        int roleID=player.getID();
        String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
        TRole tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);
        String area_prefix=ModelCacheService.AREA_PREFIX(roleID);
        List<TRoleArea> areaList= (List<TRoleArea>) modelCacheService.findListByPrefixID(area_prefix);
        TRoleArea roleArea=null;
        for (TRoleArea area:areaList){
            if (area.getRoleareaid()==areaID){
                roleArea=area;
                areaList.remove(area);
                break;
            }
        }
        achiveAllStatrsRewardResponse.Builder builder=achiveAllStatrsRewardResponse.newBuilder();
        if (roleArea!=null){
            int  achiveStatus=roleArea.getAllstartsreword();//区域奖励领取状态1.未领取，2.已领取，3.未达三星
            if (achiveStatus== 0x1){
               try {
                   //领取丹药奖励
                   int recoverySoul= ScriptFactory.stateModelFastMap.get(tRole.getStateid()).getRoleSualRecovery(); //
                   tRole.setRolesual(tRole.getRolesual()+recoverySoul); //丹药回复
                   player.sendSoulTime(tRole,0);
                   //auto recovery soul
                   player.autoRecoverySoul(tRole);
                   //update soul db and cache
                   playerService.updateRole(tRole);
                   modelCacheService.addObjectPrefixID(role_prefix,tRole);

                   roleArea.setAllstartsreword((byte)0x2);
                   playerService.updateArea(roleArea);
                   areaList.add(roleArea);
                   modelCacheService.addListByPrefixID(area_prefix,areaList);

                   builder.setAreaID(areaID);
                   builder.setAreastatus(0x2);
                   builder.setStatus(0x1);
                   builder.setSoul(tRole.getRolesual());
                   builder.setMaxSoul(tRole.getRolemaxsual());
               }catch (dataException e){
                  builder.setStatus(0x2);
               }
            } else {
                builder.setStatus(0x2);
            }
        }
        player.DirectSend(wrappedBufferShort(achiveAllStatrsRewardResponse.msgID.ID_VALUE,builder.build()));
    }
}
